<template>
  <el-form
    :inline="inline"
    :show-message="showMessage"
    :rules="rules"
    :model="model"
    @submit.native.prevent
    ref="formRef"
    v-bind="$attrs"
  >
    <el-row v-if="validFields.find((it: FieldsItem) => it.span)">
      <template v-for="item in validFields">
        <!-- custom 自定义内容 -->
        <slot v-if="item.custom" :name="item.prop"></slot>
        <el-col :span="item.span" v-else-if="item.span">
          <el-form-item
            :label="item.label"
            :prop="item.prop"
            :rules="formattRules(item)"
            v-bind="{ ...item }"
            v-if="!item.subassembly"
          >
            {{ model[item.prop] }}
          </el-form-item>
          <el-form-item v-else :label="item.label" :prop="item.prop" :rules="formattRules(item)" v-bind="{ ...item }">
            <AutoComponents
              v-model="model[item.prop]"
              :is="item.subassembly"
              v-bind="{ ...item.subOptions }"
              v-model:inputlabel="model[item.subOptions?.inputlabel]"
              v-on="item.subOptions?.event ? item.subOptions.event : {}"
              :subOptions="item.subOptions"
            />
            <slot :name="`${item.prop}behind`"></slot>
          </el-form-item>
        </el-col>

        <el-form-item v-bind="{ ...item }" v-else-if="!item.subassembly">
          {{ model[item.prop] }}
        </el-form-item>

        <el-form-item
          :label="item.label"
          :prop="item.prop"
          :label-width="item.labelWidth"
          :rules="formattRules(item)"
          v-bind="{ ...item }"
          v-else
        >
          <AutoComponents
            v-model="model[item.prop]"
            :is="item.subassembly"
            v-bind="{ ...item.subOptions }"
            v-model:inputlabel="model[item.subOptions?.inputlabel]"
            v-on="item.subOptions?.event ? item.subOptions.event : {}"
            :subOptions="item.subOptions"
          />
        </el-form-item>
      </template>
      <slot name="btnGroup"></slot>
    </el-row>

    <template v-else>
      <template v-for="item in validFields">
        <!-- custom 自定义内容 -->
        <slot v-if="item.custom" :name="item.prop"></slot>

        <el-form-item v-bind="{ ...item }" v-else-if="!item.subassembly">
          {{ model[item.prop] }}
        </el-form-item>

        <el-form-item
          :label="item.label"
          :prop="item.prop"
          :label-width="item.labelWidth"
          :rules="formattRules(item)"
          v-bind="{ ...item }"
          v-else
        >
          <AutoComponents
            v-model="model[item.prop]"
            :is="item.subassembly"
            v-bind="{ ...item.subOptions }"
            v-model:inputlabel="model[item.subOptions?.inputlabel]"
            v-on="item.subOptions?.event ? item.subOptions.event : {}"
            :subOptions="item.subOptions"
          />
        </el-form-item>
      </template>
      <slot name="btnGroup"></slot>
    </template>
  </el-form>
</template>

<script lang="ts">
  import { defineComponent, ref, PropType, computed, type Component } from 'vue'
  import AutoComponents from '../../v-table/src/autoComponents'
  import type { FormInstance, FormRules } from 'element-plus'
  import { FieldsItem, VFormProps } from './vform'

  export interface Item {
    item: any
    prop: string
    subassembly: String
  }

  export default defineComponent({
    name: 'VForm',
    components: {
      AutoComponents,
    },
    props: {
      fields: {
        type: Array,
        default: Array as PropType<FieldsItem[]>,
      },
      inline: {
        type: Boolean,
        default: false,
      },
      model: {
        type: Object,
        default: () => {},
      },
      rules: {
        type: Object,
        default: () => {},
      },
      showMessage: {
        type: Boolean,
        default: false,
      },
    },
    setup(props: VFormProps) {
      const formRef = ref()
      function reset() {
        formRef.value.resetFields()
      }

      const validate = () => {
        return new Promise((resolve, reject) => {
          return formRef.value.validate((valid: any, fields: any) => {
            resolve(valid)
          })
        })
      }

      // 是否设置单独行验证规则
      const formattRules = (item: any) => {
        let rules =
          item.subOptions?.rules || item.required
            ? [{ required: true, trigger: ['change', 'blur'], message: `${item.label}不能为空` }]
            : null

        return rules
      }

      function resetFields() {
        formRef.value.resetFields()
      }

      const formatterProp = (prop: unknown) => {
        console.log('formatterProp', prop, props)
      }

      // 过滤隐藏的列
      const validFields = computed(() => props.fields.filter((it: any) => !it.hidden))

      return {
        formRef,
        reset,
        resetFields,
        formattRules,
        validate,
        formatterProp,
        validFields,
      }
    },
  })
</script>

<style lang="scss" scoped>
  .el-input,
  .el-input-number,
  .el-select,
  .el-textarea,
  .el-input__wrapper {
    width: 100%;
  }
</style>
